import {
  Controller,
  Get, Post,
  HttpException, HttpStatus,
  Param, Query, Body,
  ParseIntPipe, ParseFloatPipe, ParseBoolPipe, ParseArrayPipe, ParseUUIDPipe, ParseEnumPipe,DefaultValuePipe, ValidationPipe,
  UsePipes,
} from '@nestjs/common'

// 自定义管道
import { customPipe } from './custom.pipe'
import { ZodValidationPipe } from './zod-validation.pipe' // 基于对象的架构验证
import { ClassValidationPipe } from './class-validation.pipe' // 基于类的架构验证

// 获取 dto
import { 
  createCatSchema, // 获取 schema 
  CreateCatDto, // 获取 schema 推断出来的类型
} from './create-cat.dto'
import { CreateUserDto } from './create-user.dto'

// 枚举
enum Roles {
  Admin = 'ADMIN',
  Vip ='VIP'
}
const a = Roles.Admin
console.log(a)

@Controller()
// @UseFilters( CustomExceptionFilter) // 控制器上使用自定义异常过滤器 应用于该控制器中所有路由处理函数
// @UsePipes(new ValidationPipe) // 在控制器上使用管道 
export class AppController {
  constructor(
  ) { }

  @Get("number/:id")
  handle(@Param("id", ParseIntPipe) id: number) {
    console.log('id: ', id, typeof id)
    return `number: ${id}`
  }


  @Get('float/:value')
  getFloat(@Param('value', ParseFloatPipe) value: number) {
    console.log('value: ', value, typeof value)
    return `value: ${value}`
  }

  @Get('bool/:flag')
  getBool(@Param('flag', ParseBoolPipe) flag: boolean) {
    console.log('flag: ', flag, typeof flag)
    return `flag: ${flag}`
  }

  @Get('array/:values')
  getArray(@Param('values', new ParseArrayPipe({ items: Number, separator: '@' })) arr: any[]) {
    // 默认转换分割符是 ,
    console.log('arr: ', arr)
    return `arr: ${arr}`
  }

  @Get('uuid/:id')
  getUUID(@Param('id', ParseUUIDPipe) id: string) { // 92f62d9e-22c4-11ef-97e9-325096b39f47 转换为 uuid
    console.log('id: ', id)
    return `id: ${id}`
  }

  @Get('admin/:role')
  getRole(@Param('role', new ParseEnumPipe(Roles)) role: Roles) { // role 参数只能是Roles枚举属性中的值
    console.log('role: ', role)
    return `role: ${role}`
  }

  @Get('default')
  getDefault(@Query('username', new DefaultValuePipe('zs')) username: string) {
    console.log(`username: ${username}`, typeof username)
    return `username: ${username}`
  }

  @Get('custom/:value')
  getCustom(@Param('value', customPipe) value: string) {
    return value
  }

  // 基于对象的架构验证
  @Post('cats')
  @UsePipes(new ZodValidationPipe(createCatSchema))
  async getCats(@Body() createCat: CreateCatDto) {
    console.log('createCat: ', createCat)
    return createCat
  }

  // 基于类架构的验证
  @Post('create/user')
  @UsePipes(new ClassValidationPipe)
  async createUser(@Body() createUser: CreateUserDto) {
    console.log('createUser: ', createUser)
    return createUser
  }

  @Post('create/user2')
  async createUser2(@Body() createUser: CreateUserDto) {
    console.log('createUser: ', createUser)
    return createUser
  }

  @Post('create/user3')
  async createUser3(@Body() createUser) {
    console.log('createUser ', createUser)
    return createUser
  }
}
